﻿
//Author: 张亚涛
//createtime:2011-10-13 10:35:35
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Model;
using NS.DAL.Systems;


namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "CarType" 表的方法;
    /// </summary>
    public class CommuntyBase
    {
        const string SQL_Select = "SELECT [CommuntyID],[CommuntyName],[AreaNumber] FROM [Communty] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回CarType表的所有数据
        /// </summary>
        /// <param name="CommuntyName">小区名称</param>
        /// <returns></returns>        
        public static List<Communty> GetAllCommunty(string CommuntyName)
        {
            List<Communty> CommuntyList = new List<Communty>();
            SqlDataReader reader = null;

            string sql = SQL_Select + " where 1=1 ";
            if (!string.IsNullOrEmpty(CommuntyName))
            {
                sql += " and CommuntyName like '%" + CommuntyName + "%'";
            }
            sql += "order by CommuntyName";
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                Communty Communty = new Communty(reader);
                CommuntyList.Add(Communty);
            }
            reader.Close();
            return CommuntyList;
        }

        /// 根据Communty表的CommuntyID字段返回数据 
        /// </summary>
        /// <param name="CommuntyID">小区ID</param>
        /// <returns></returns>       
        public static Communty GetCommuntyByCommuntyID(int CommuntyID)
        {
            Communty communty = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [CommuntyID] = @CommuntyID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@CommuntyID",CommuntyID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                communty = new Communty(reader);
            }
            reader.Close();
            return communty;
        }
        /// <summary>
        /// 根据小区ID返回小区名称 
        /// </summary>
        /// <param name="CommuntyID">车型ID</param>
        /// <returns></returns>       
        public static string GetCommuntyNameByID(int CommuntyID)
        {
            string sql = "SELECT [CommuntyName] FROM [Communty] WHERE [CommuntyID] = @CommuntyID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@CommuntyID",CommuntyID)
            };
            return SQLHelper.ExecuteScalar(sql, paras);
        }
        /// <summary>
        /// 返回员工负责小区名字  
        /// </summary>
        /// <param name="WeChat">员工微信号</param>
        /// <returns></returns>        
        public static string GetComNameStr(string WeChat)
        {
            string resStr = "";
            string sql = "select [CommuntyName] from [Communty]";
            string comstr = UaerComBase.GetComInStr(WeChat);
            if (!string.IsNullOrEmpty(comstr))
            {
                sql += " where CommuntyID in(" + comstr + ") ";
            }
            else
            {
                sql += " where 1=0 ";
            }
            SqlDataReader reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                resStr += reader[0].ToString() + "、";
            }
            reader.Close();
            if (!string.IsNullOrEmpty(resStr))
            {
                resStr = resStr.Substring(0, resStr.Length - 1);
            }
            return resStr;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据Communty表的AreaNumber字段返回数据 
        /// </summary>
        /// <param name="AreaNumber">地区ID</param>
        /// <returns></returns>        
        public static int DeleteCommuntyByCommuntyID(long CommuntyID)
        {
            string sql = "delete from [Communty] where [CommuntyID] = @CommuntyID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@CommuntyID",CommuntyID)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(Communty Communty)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@CommuntyID",Communty.CommuntyID),
            new SqlParameter("@CommuntyName",Communty.CommuntyName),
            new SqlParameter("@AreaNumber",Communty.AreaNumber),
            };
            return paras;
        }
        /// <summary>
        /// 根据Communty表的CommuntyID字段更新数据 
        /// </summary> 
        /// <param name="Communty">Communty</param>
        /// <returns></returns>       
        public static int UpdateCommuntyByCommuntyID(Communty Communty)
        {
            string sql = "update [Communty] set [CommuntyName] = @CommuntyName,[AreaNumber] = @AreaNumber where [CommuntyID] = @CommuntyID";
            SqlParameter[] paras = PrepareCommandParameters(Communty);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向Communty表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="Communty">Communty</param>       
        /// <returns></returns>        
        public static int AddCommunty(Communty Communty)
        {
            string sql = "insert into [Communty]([CommuntyName],[AreaNumber])values(@CommuntyName,@AreaNumber);select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(Communty);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回CarType表的所有数据 （SQL语句）
        /// <param name="CommuntyName">小区名称</param>
        /// </summary>
        /// <returns></returns>        
        public static string GetAllCommunty_SQL(string CommuntyName)
        {
            string strSQL = SQL_Select + " where 1=1 ";
            if (!string.IsNullOrEmpty(CommuntyName))
            {
                strSQL += " and CommuntyName like '%" + CommuntyName + "%'";
            }
            return strSQL;
        }
        #endregion
    }
}
